﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using RoyTools.D3Monitor.FormUI.Properties;

namespace RoyTools.D3Monitor.FormUI.Loot
{
    /// <summary>
    /// 掉落分析类
    /// </summary>
    public class LootAnalyzer
    {
        /// <summary>
        /// 更新分析结果
        /// </summary>
        /// <param name="fileInfo"></param>
        /// <returns></returns>
        public static LootAnalyzerResultEntity UpdateAnalyzerResult(FileInfo fileInfo)
        {
            var lootAnalyzerResult = ReadLog(fileInfo);
            return CalculateLootAnalyzerResult(lootAnalyzerResult);
        }

        /// <summary>
        /// 读取日志
        /// </summary>
        /// <param name="fileInfo"></param>
        /// <returns></returns>
        private static LootAnalyzerResultEntity ReadLog(FileInfo fileInfo)
        {
            LootAnalyzerResultEntity lootAnalyzerResult = new LootAnalyzerResultEntity();
            var canRead = false;
            while (!canRead)
            {
                using (var streamReader = fileInfo.OpenText())
                {
                    bool isFirstTime = false;
                    while (!streamReader.EndOfStream)
                    {
                        var line = streamReader.ReadLine().ToLower();

                        var timeString = Regex.Match(line, @"\d+:\d+:\d+").Value;
                        if (!string.IsNullOrWhiteSpace(timeString))
                        {

                            var year = fileInfo.CreationTime.Year;
                            var month = fileInfo.CreationTime.Month;
                            var day = fileInfo.CreationTime.Day;
                            var hour = int.Parse(line.Split(',')[0].Split(':')[0].Trim());
                            var minute = int.Parse(line.Split(',')[0].Split(':')[1].Trim());
                            var second = int.Parse(line.Split(',')[0].Split(':')[2].Trim());
                            if (!isFirstTime)
                            {
                                lootAnalyzerResult.LogFirstTime = new DateTime(year, month, day, hour, minute, second);
                                isFirstTime = true;
                            }
                            lootAnalyzerResult.LogLastTime = new DateTime(year, month, day, hour, minute, second);
                        }
                        if (line.Contains(Resources.RareDropString))
                        {
                            lootAnalyzerResult.ItemFound++;
                            lootAnalyzerResult.RareFound++;
                        }
                        if (line.Contains(Resources.SetDropString))
                        {
                            lootAnalyzerResult.ItemFound++;
                            lootAnalyzerResult.LegendaryFound++;
                        }
                    }
                    streamReader.Close();
                    streamReader.Dispose();
                    canRead = true;
                }
            }
            return lootAnalyzerResult;
        }

        /// <summary>
        /// 计算掉落分析结果
        /// </summary>
        /// <returns></returns>
        private static LootAnalyzerResultEntity CalculateLootAnalyzerResult(LootAnalyzerResultEntity lootAnalyzerResult)
        {
            lootAnalyzerResult.ItemFoundPerHour = CalculatePerHour(lootAnalyzerResult.LogLastTime, lootAnalyzerResult.LogFirstTime, lootAnalyzerResult.ItemFound);
            lootAnalyzerResult.LegendaryFoundPerHour = CalculatePerHour(lootAnalyzerResult.LogLastTime, lootAnalyzerResult.LogFirstTime, lootAnalyzerResult.LegendaryFound);
            lootAnalyzerResult.RareFoundPerHour = CalculatePerHour(lootAnalyzerResult.LogLastTime, lootAnalyzerResult.LogFirstTime, lootAnalyzerResult.RareFound);

            lootAnalyzerResult.LegendaryFoundRate = CalculateDropRate(lootAnalyzerResult.ItemFound, lootAnalyzerResult.LegendaryFound);
            lootAnalyzerResult.RareFoundRate = CalculateDropRate(lootAnalyzerResult.ItemFound, lootAnalyzerResult.RareFound);

            return lootAnalyzerResult;
        }

        /// <summary>
        /// 计算每小时掉落
        /// </summary>
        /// <param name="divisor"></param>
        /// <returns></returns>
        private static decimal CalculatePerHour(DateTime lastTime, DateTime firstTime, int divisor)
        {
            if (divisor > 0)
            {
                var timeSpan = lastTime - firstTime;
                var rateResult = decimal.Parse(timeSpan.TotalSeconds.ToString()) / 60 / 60;
                var rate = decimal.Round(rateResult, 2);
                var result = decimal.Parse(divisor.ToString()) / rate;
                return decimal.Round(result, 2);
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 计算掉落率
        /// </summary>
        /// <param name="divisor"></param>
        /// <returns></returns>
        private static decimal CalculateDropRate(int dividend, int divisor)
        {
            if (dividend > 0 && divisor > 0)
            {
                var result = decimal.Parse(divisor.ToString()) / decimal.Parse(dividend.ToString());
                return decimal.Round(result * 100, 2);
            }
            else
            {
                return 0;
            }
        }

    }
}
